import { createRouter, createWebHistory } from "vue-router";
import { ElMessage } from 'element-plus';
import store from '../store'
//首页
import Home from '../components/home/Home';
	//首页默认数据
	import Main from '../components/home/page/Main';
	//详情
	import Details from '../components/home/page/Details';
	//我的订单
	import MyOrder from '../components/home/page/MyOrder';
	//个人信息设置
	import Myset from '../components/home/page/Myset';
	//购物车
	import Cart from '../components/home/cart/Cart';
		//购物车首页
		import Cart_Main from '../components/home/cart/page/Main';
		//提交订单
		import Cart_Make from '../components/home/cart/page/Make';
		//订单付款
		import Cart_Pay from '../components/home/cart/page/Pay';

//后台
import Admin from '../components/admin/Admin';
	//后台欢迎页
	import Welcome from '../components/admin/page/Welcome';
	//热点管理
	import Banner from '../components/admin/page/Banner';
	//书籍管理
	import Book from '../components/admin/page/Book';
	//类型管理
	import Type from '../components/admin/page/Type';
	//类型管理(多级)
	import Type2 from '../components/admin/page/Type2';
	//书本添加、修改
	import BookEdit from '../components/admin/page/BookEdit';
	//轮播图添加
	import AddBanner from '../components/admin/page/AddBanner';
	//订单管理
	import Order from '../components/admin/page/Order';
	//用户列表
	import User from '../components/admin/page/User';
	//管理员列表
	import AdminUser from '../components/admin/page/AdminUser';

const routes = [
	{path:'/',redirect:'home'},
	{path:'/home',name:'home',component:Home,
		children:[
			{path:'', 		 name:'main', 	 component:Main},
			{path:'details', name:'details', component:Details},
			{path:'myorder', name:'myorder', meta:{UserAuth:true} ,component:MyOrder},
			{path:'myset',   name:'myset', 	 meta:{UserAuth:true} ,component:Myset},
			{path:'cart',	 name:'cart', 	 meta:{UserAuth:true} ,component:Cart,
				children:[
					{path:'', 	  name:'cart_main', component:Cart_Main},
					{path:'make', name:'cart_make', component:Cart_Make},
					{path:'pay', name:'cart_pay', component:Cart_Pay}
				]
			}
		]
	},
	{path:'/admin', component:Admin, meta:{UserAuth:true},
		children:[
			{path:'',   	  name:'welcome', 	component:Welcome},
			{path:'banner',   name:'banner', 	component:Banner},
			{path:'book', 	  name:'book',		component:Book},
			{path:'type',	  name:'type', 		component:Type},
			{path:'type2',	  name:'type2', 	component:Type2},
			{path:'bookedit', name:'bookedit',  component:BookEdit},
			{path:'hotedit',  name:'hotedit',   component:AddBanner},
			{path:'order',    name:'order',   	component:Order},
			{path:'user', 	  name:'user',   	component:User},
			{path:'adminuser',name:'adminuser', component:AdminUser},
		]
	}
];


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});


//路由守护
//to:即将前往的路径
//from：即将离开的路径
router.beforeEach((to,from,next)=>{
	//必须要用户登录
	// console.log(to,from);
	if(to.meta.UserAuth){
		//用户已登录
		if(store.getters.hasOnline){
			//正常跳转
			next();
		}else{
			//用户未登录,跳转回原来的页面
			ElMessage.error({message: '请先登录！'});
			next({path:from.path});
		}
	}else{
		//正常跳转
		next();
	}
})


export default router;
